<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/
 
class KomaSessionHandler extends KomaSingletonObject
{    
    function __construct()
    {
        $this->createProperties(array('Lifetime' => ini_get('session.gc_maxlifetime')
                                   ));
        
        $this->setReadOnlyProperties('Lifetime');
        
        session_set_save_handler
        (
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
        );
        
        register_shutdown_function('session_write_close');
        session_start();
    }
 
    function regenerateID()
    {
        $oldSessionID = session_id();
        session_regenerate_id();
        $this->destroy($oldSessionID);
    }
 
    function open($save_path, $session_name)
    {
        return true;
    }
 
    function close()
    {
        return true;
    }
 
    function read($session_id)
    {
        $komaDB = KomaDatabaseFactory::getInstance();
        
        $result = $komaDB->query("SELECT session_data
                                 FROM ".$komaDB->prefix('session')."
                                 WHERE session_id = '".$session_id."' AND http_user_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND session_expire > '".time()."'");
 
        if (is_resource($result) && $komaDB->numRows($result) > 0)
        {
            $fields = $komaDB->fetchAssoc($result);
            return $fields["session_data"];
        }
 
        return '';
    }
 
    function write($session_id, $session_data)
    {
        $komaDB = KomaDatabaseFactory::getInstance();
        
        $result = $komaDB->query("SELECT *
                                FROM ".$komaDB->prefix('session')."
                                WHERE session_id = '".$session_id."'");
 
        if ($komaDB->numRows($result) > 0)
        {
            $result = $komaDB->query("UPDATE ".$komaDB->prefix('session')."
                                   SET session_data = '".$session_data."', session_expire = '".(time() + $this->Lifetime)."'
                                   WHERE session_id = '".$session_id."'");
 
            if ($komaDB->affectedRows())
            {
                return true;
            }
         }
         else
         {
            $result = $komaDB->query("INSERT INTO ".$komaDB->prefix('session')."
                                    (session_id, http_user_agent, session_data, session_expire)
                                    VALUES
                                    (
                                    '".$session_id."',
                                    '".$_SERVER["HTTP_USER_AGENT"]."',
                                    '".$session_data."',
                                    '".(time() + $this->Lifetime)."'
                                    )
            ");
 
            if ($komaDB->affectedRows())
            {
                return true;
            }
        }
 
        return false;
    }
 
    function destroy($session_id)
    {
        $komaDB = KomaDatabaseFactory::getInstance();
        
        $result = $komaDB->query("DELETE
                                 FROM ".$komaDB->prefix('session')."
                                 WHERE session_id = '".$session_id."'");
 
        if ($komaDB->affectedRows())
        {
            return true;
        }
 
        return false;
    }
 
    function gc($maxlifetime)
    {
        $komaDB = KomaDatabaseFactory::getInstance();
        
        $result = $komaDB->query("DELETE
                                 FROM ".$komaDB->prefix('session')."
                                 WHERE session_expire < '".(time() - $maxlifetime)."'");
        
    }
    
    static function getInstance()
    {
        return self::createInstance(__CLASS__);
    }
}
?>